package com.hatricks.is.base.stream.progressinputstream;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.nio.file.Files;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;

/**
 * @packageName: com.hatricks.base.stream.progressinputstream
 * @className: ProgressStreamTest
 * @author: yanyh yanyh@glodon.com
 * @date: 2024/3/27 14:47
 * @description:
 */
@SuppressWarnings("VulnerableCodeUsages")
public class ProgressStreamTest {
    public static void main(String[] args) {
        InputStream inputStream = FileUtil.getInputStream("C:\\Users\\glodon\\Desktop\\1-IT服务指南.pptx");
        InputStream progressInputStream = new ProgressInputStream(inputStream, x -> {
            if (ProgressEventType.TRANSFER_FINISHED.equals(x.getEventType())) {
                System.out.println("上传结束啦");
            }
            if (ProgressEventType.TRANSFER_START.equals(x.getEventType())) {
                System.out.println("上传开始啦");
            }
            System.out.println(x.getBytes());
        });
        IoUtil.readUtf8(progressInputStream);




        //private static ProgressContext startUploadProgress(FileUploadParams uploadParams, List< MultipartFile > files) {
        //    Long totalFileSize = files.stream().mapToLong(MultipartFile::getSize).boxed().reduce(Long::sum).orElse(0L);
        //    return ProgressHelper.startProgress(totalFileSize, Boolean.TRUE, pc -> {
        //        if (!pc.getExceptionSignal().get()) {
        //            RedisUtil.getInstance().setString(RedisKeyEnum.ZJY_UPLOAD_FILE_PROGRESS, String.valueOf(pc.calculateUploadProgress()), uploadParams.getUploadProgress());
        //        }
        //    });
        //}

        // catch (Exception e) {
        //    log.error("当前文件上传异常,异常信息为:[{}], 异常栈帧为", e.getMessage(), e);
        //    progressContext.buildException();
        //    calculateProgressFailed(uploadParams.getUploadProgress());
        //}
    }

    private static final String DOC_PROCESS_THREAD_POOL = "doc-process-thread-pool";

    @Bean("doc-process-thread-pool")
    public ScheduledThreadPoolExecutor docScheduledProcessThreadPool() {
        ThreadFactoryBuilder builder = ThreadFactoryBuilder.create();
        builder.setThreadFactory((runnable) -> {
            // 异步线程池的上下文传递
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes == null) {
                return new Thread(runnable);
            }
            RequestAttributes context = RequestContextHolder.currentRequestAttributes();
            return new Thread(() -> {
                try {
                    RequestContextHolder.setRequestAttributes(context);
                    runnable.run();
                } finally {
                    RequestContextHolder.resetRequestAttributes();
                }
            });
        });
        ThreadFactory threadFactory = builder.setDaemon(Boolean.FALSE).setNamePrefix(DOC_PROCESS_THREAD_POOL).build();
        return new ScheduledThreadPoolExecutor(50, threadFactory);
    }
}
